<template>
    <div>
        <van-nav-bar safe-area-inset-top placeholder :title="$t('BindNo.')" left-arrow fixed @click-left="onClickLeft" />
        <van-form @submit="onSubmit">
            <div class="scanBox">
                <div class="innerBox" v-if="from=='/main/inverter'">
                    <div class="tit required">{{$t('Site')}}</div>
                    <div class="select" @click="showPicker2 = true" >
                        <div class="tit_1">{{siteName}}</div>
                        <div class="tit_2"><van-icon name="arrow-down" /></div>
                    </div>
                </div>
                <van-popup v-model="showPicker2" round position="bottom">
                <van-picker
                    show-toolbar
                    :columns="siteListData"
                    value-key='siteName'
                    @cancel="showPicker2 = false"
                    @confirm="onConfirm2"
                    :confirm-button-text="$t('OK')"
                    :cancel-button-text="$t('Cancel')"
                />
                </van-popup>
                <div class="innerBox">
                    <div class="tit required">{{$t('RegistrationNo')}}</div>
                    <div class="select">
                        <div class="tit_3">
                            <van-field class="input-sn" type="text" v-model='inputSn' />
                        </div>
                        <div class="tit_4" @click="goCamera"><van-icon name="scan" /></div>
                    </div>
                </div>
            </div>
            <div class="btnBox">
                <van-button color="linear-gradient(#F9C25F, #F5861A)" :disabled="inputSn==''||inputSn==undefined" round block native-type="submit">
                    {{$t('OK')}}
                </van-button>
            </div>
        </van-form>
        <div class="example">
            <img src="../../assets/example.png" alt="">
        </div>
    </div>
</template>

<script>
import { Toast } from 'vant';
export default {
    data() {
        return {
            from:sessionStorage.getItem('setFrom'),
            inputSn:this.$route.query.sn,
            showPicker2:false,
            siteName:'',
            siteId:'',
            siteListData:[]
        };
    },
    mounted(){
        this.getSiteList();
    },
    methods: {
        onClickLeft(){
            this.$router.push({path:this.from});
        },
        async getSiteList(){
            if(this.from=='/main/inverter'){
                let {result:{rows}} = await this.API.siteList(localStorage.getItem('domain'),{
                    'tokenId':localStorage.getItem('tokenId'),
                    'current':1,
                    'size':1000,
                    'siteName':''
                })
                this.siteListData = rows
                this.siteName = rows.length&&rows[0].siteName
                this.siteId = rows.length&&rows[0].ids
            }else{
                this.siteId=localStorage.getItem('siteId');
                this.siteName=localStorage.getItem('siteName');
            }
        },
        onConfirm2(value){
            this.showPicker2 = false
            this.siteName = value.siteName
            this.siteId = value.ids
        },
        goCamera(){
            this.$router.push({path:'/camera',query:{code:2}});
        },
        async onSubmit(){
            this.comm.loadingShow();
            let data = await this.API.vididateWifiSnAdd(localStorage.getItem("domain"),{
                'wifiSn':this.inputSn,
                'siteId':this.siteId,
                'userId':localStorage.getItem('userId')
            });
            if(data.exception==0){
                this.bindSn();
            }else{
                this.comm.tipsShow('SNinvalid');
            }
        },
        async bindSn(){
            this.comm.loadingShow();
            let data = await this.API.BindSn(localStorage.getItem("domain"),{
                'tokenId':localStorage.getItem('tokenId'),
                'userId':localStorage.getItem('userId'),
                'siteId':this.siteId,
                'siteName':encodeURI(this.siteName),
                'userName':encodeURI(localStorage.getItem('userName')),
                'wifiSns':this.inputSn,
                'packageIds':''
            });
            if(data.success){
                Toast.success(this.$t('BindSuccess'));
            }else{
                Toast.fail(this.$t('BindFaild'));
            }
        }
    }
};
</script>

<style  lang="less" scoped>
.scanBox{
    width: 100%;
    background: #fff;
    .innerBox{
        width: 100%;
        border-bottom: 1px solid #efefef;
        padding: 8px 8px 0 8px;
        box-sizing: border-box;
        .tit{
            width: 100%;
            text-align: left;
            color: #929292;
            height: 30px;
            line-height: 30px;
        }
        .select{
            width: 100%;
            height: 40px;
            display: flex;
            justify-content: space-between;
            .tit_1{
                line-height: 40px;
            }
            .tit_2{
               margin-top: 12px;
            }
            .tit_3{
                width: 80%;
                line-height: 40px;
            }
        }
    }
    .input-sn{
        width: 100%;
        height: 40px;
        padding-left: 0;
    }
    .tit_4{
        font-size: 30px;
        margin-top: 5px;
        color: #F6872D;
    }
}
.next-button{
    width: 60%;
    text-align: center;
    margin: 0 auto;
    background-color: #B7B8C1;
    padding: 10px 0px;
    border-radius: 10px;
    margin-top:25px;
    color:#fff;
}
.active{
    background-color: #5D606E;
}
.input-sn{
    text-align:center;
}
.example{
    width: 80%;
    margin: 40px auto;
    img{
        width: 100%;
        height: auto;
    }
}
</style>
